﻿using Alipay.AopSdk.Core;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core.Response;
using Alipay.AopSdk.Core.Util;
using OtherLib.AliPay.Dto;
using System;
using System.Collections.Generic;
using System.Text;

namespace OtherLib.AliPay
{
    public class AliPayUtil
    {
        public static AliPayTransferDto AliPayTransfer(string out_biz_no, string payee_account, string payee_real_name, string amount, string remark)
        {
            IAopClient client = new DefaultAopClient(AliPayConfig.Getewayurl, AliPayConfig.AppId, AliPayConfig.APP_PRIVATE_KEY, "json", "1.0",
                                                    AliPayConfig.SignType, AliPayConfig.ALIPAY_PUBLIC_KEY, "UTF-8", false);
            AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
            request.BizContent = "{" + "\"out_biz_no\":\"" + out_biz_no + "\"," + "\"payee_type\":\"ALIPAY_LOGONID\"," +
                    "\"payee_account\":\"" + payee_account + "\"," + "\"amount\":\"" + amount + "\"," + "\"payer_show_name\":\"金华市够莱富科技有限公司\"," +
                    "\"payee_real_name\":\"" + payee_real_name + "\"," + "\"remark\":\"" + remark + "\"" + "}";
            request.SetNotifyUrl(AliPayConfig.NotifyUrl);
            AliPayTransferDto aliPlay = new AliPayTransferDto();
            try
            {
                AlipayFundTransToaccountTransferResponse response = client.Execute(request);
                //调用成功，则处理业务逻辑         
                aliPlay.isError = response.IsError;
                aliPlay.body = response.Body;
                aliPlay.sub_msg = response.SubMsg;
                aliPlay.out_biz_no = response.OutBizNo;
                if (!response.IsError)
                {
                    aliPlay.order_id = response.OrderId;
                    aliPlay.pay_date = response.PayDate;
                }
            }
            catch (Exception ex)
            {
                aliPlay.isError = true;
                aliPlay.sub_msg = ex.Message;
            }
            return aliPlay;
        }

        /// <summary>
        /// 支付宝支付
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="payMoney">付款金额</param>
        /// <param name="Subject">商品的标题/交易标题/订单标题/订单关键字等</param>
        /// <param name="TradeNo">商家订单号</param>
        /// <param name="Body">具体描述信息</param>
        /// <param name="NotifyUrl">回调地址</param>
        /// <returns></returns>
        public static string AliPlay(string payMoney, string tradeNo, string subject, string body, string notifyUrl = null)
        {
            IAopClient client = new DefaultAopClient(AliPayConfig.Getewayurl, AliPayConfig.AppId, AliPayConfig.APP_PRIVATE_KEY, "json", "1.0",
                                                    AliPayConfig.SignType, AliPayConfig.ALIPAY_PUBLIC_KEY, "UTF-8", false);
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.Body = body;
            model.Subject = subject;
            model.TotalAmount = payMoney;
            model.ProductCode = "QUICK_MSECURITY_PAY";
            model.OutTradeNo = tradeNo;
            model.TimeoutExpress = "30m";
            request.SetBizModel(model);
            //request.SetNotifyUrl("https://dev.taohuikeji.com:8443");

            if (string.IsNullOrWhiteSpace(notifyUrl))
            {
                notifyUrl = AliPayConfig.NotifyUrl;
            }
            request.SetNotifyUrl(notifyUrl);
            //使用sdkExecute
            AlipayTradeAppPayResponse response = client.SdkExecute(request);
            return response.Body;
        }

        public static bool RSACheckV1(Dictionary<string, string> package)
        {
            return AlipaySignature.RSACheckV1(package, AliPayConfig.ALIPAY_PUBLIC_KEY, AliPayConfig.CharSet, AliPayConfig.SignType, false);
        }
    }
}
